package com.ylfstu.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSys {

    //1.先完成显示菜单，并可以选择菜单，给出对应提示
    //2.完成零钱通明细:1.可以把收益入账和消费，保存到数组 2.可以使用对象 3.可以用String拼接
    //3.完成收益入账
    //4.消费
    //5.退出
    //6.用户退出，给出判断，确认用户是否真的要退出
    //7.在收益入账和消费时，判断金额是否合理
    public static void main(String[] args) {

        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = "";

        //2.完成零钱通明细
        String details = "----------------零钱通明细----------------";

        //3.完成收益入账
        double money = 0;
        double balance = 0;
        Date date = null;//date 是 java.util.Date 类型，表示日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//用于日期格式化

        //4.消费
        String note = "";
        do{
            System.out.println("\n================零钱通菜单================");
            System.out.println("\t\t\t\t1 零钱通明细");
            System.out.println("\t\t\t\t2 收益入账");
            System.out.println("\t\t\t\t3 消   费");
            System.out.println("\t\t\t\t4 退   出");
            System.out.println("请选择(1~4):   ");
            key = scanner.next();

            switch(key){
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.println("收益入账金额:");
                    money = scanner.nextDouble();
                    //校验money的值
                    if (money <= 0) {
                        System.out.println("收益入账金额 需要 大于0");
                        break;
                    }

                    balance += money;
                    //拼接收益入账信息到 details
                    date = new Date();
                    details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "3":
                    System.out.println("消费金额:");
                    money = scanner.nextDouble();
                    //校验money
                    if (money <= 0 || money >= balance) {
                        System.out.println("消费金额 需要在0-" + balance + "之间");
                        break;
                    }

                    System.out.println("消费说明:");
                    note = scanner.next();
                    balance -= money;
                    //拼接消费信息到details
                    details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "4":
                    //  建议一段代码完成一个小功能，高内聚，低耦合
                    String choice = "";
                    while(true){
                        System.out.println("你确定要退出吗？ y/n");
                        choice = scanner.next();
                        if ("y".equals(choice) || "n".equals(choice)) {
                            break;
                        } else {
                            System.out.println("请输入正确的字符    y/n");
                        }
                    }
                    if (choice.equals("y")) {
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("选择有误，请重新选择！");
            }
        }while(loop);

        System.out.println("================退出零钱通项目================");

    }
}


















